package cn.iocoder.yudao.module.shop.admin.convert;

import cn.iocoder.yudao.framework.business.basic.pojo.vo.UserSimpleVO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.api.shop.info.dto.ShopInfoDTO;
import cn.iocoder.yudao.module.shop.admin.pojo.po.ShopUser;
import cn.iocoder.yudao.module.shop.admin.pojo.vo.ShopUserRespVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@Mapper
public interface ShopUserConvert {

    ShopUserConvert INSTANCE = Mappers.getMapper(ShopUserConvert.class);

    @Mapping(target = "shop", ignore = true)
    @Mapping(target = "memberUser", ignore = true)
    @Mapping(target = "adminUser", ignore = true)
    ShopUserRespVO convert(ShopUser bean);

    PageResult<ShopUserRespVO> convertPage(PageResult<ShopUser> page);

    default PageResult<ShopUserRespVO> convertPage(PageResult<ShopUser> pageResult,
                                                   List<UserSimpleVO> users,
                                                   Map<Long, ShopInfoDTO> shops) {
        PageResult<ShopUserRespVO> page = convertPage(pageResult);
        page.getList().forEach(vo -> {
            UserSimpleVO user = users.stream().filter(u -> Objects.equals(u.getId(), vo.getMemberUserId())).findFirst().orElse(null);
            vo.setMemberUser(user);
        });
        page.getList().forEach(vo -> {
            ShopInfoDTO shop = shops.get(vo.getShopId());
            vo.setShop(shop == null ? ShopInfoDTO.FALLBACK : shop);
        });
        return page;
    }

    List<ShopUserRespVO> convertList(List<ShopUser> list);

    default List<ShopUserRespVO> convertList(List<ShopUser> list,
                                             List<UserSimpleVO> users,
                                             List<ShopInfoDTO> shops) {
        List<ShopUserRespVO> respVOList = convertList(list);
        respVOList.forEach(vo -> {
            ShopInfoDTO shop = shops.stream().filter(u -> Objects.equals(u.getId(), vo.getShopId())).findFirst().orElse(null);
            vo.setShop(shop);
            UserSimpleVO user = users.stream().filter(u -> Objects.equals(u.getId(), vo.getMemberUserId())).findFirst().orElse(null);
            vo.setMemberUser(user);
        });
        return respVOList;
    }

}
